package com.java.practise.tags;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

@SuppressWarnings("serial")
public class NonPaginated extends Base {
	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			out.print(this.nonPaginatedTable());
		}
		catch(Exception ex) {
			throw new JspException("Exception occured: " + ex.getMessage());
		}
		return EVAL_PAGE;
	}

	private StringBuffer nonPaginatedTable() throws SQLException{
		StringBuffer stringBuffer = new StringBuffer();
		ResultSetMetaData rsmd = rst.getMetaData();
		try {
			int noOfRows = this.getRowCount(rst);
		    int count = rsmd.getColumnCount(); 
		    
		    stringBuffer.append("<tr>"); 
    		if(select) {
    			stringBuffer.append("<td valign='top' align='center' style='" + this.defaultHeaderCssStyle() + "' class='" + this.headerStyle + "'>Select</td>");
    		}
		    for (int i = 1; i <= count; i++) {
	    		stringBuffer.append("<td id='" + i + "' style='" + this.defaultHeaderCssStyle() + "' class='" + this.headerStyle + "' ondblclick='toggleWrap(this.id, " + noOfRows + ")' nowrap='nowrap'>");
		    	stringBuffer.append(rsmd.getColumnName(i) + " [" + rsmd.getColumnTypeName(i).toLowerCase() + "]");
		    	stringBuffer.append("</td>");
		   	}		
	    	stringBuffer.append("</tr>");   
	    	
	    	
	    	int z = 1;
		    while(rst.next()) {
		    	stringBuffer.append("<tr valign='top' name='checkBox" + z + "' onmouseover='focusRow(this)' onmouseout='defocusRow(this)'>");
		    	/* 
		    	 * If we are seeing the content of the table.
		    	 * We need to have a select box.
		    	 */
		    	if(select) {
			    	stringBuffer.append("<td valign='top' style='" + this.defaultRowCssStyle() + "' class='" + this.rowStyle + "' align='center' width='20px'>");
			    	stringBuffer.append("<input type='checkbox' id='selectedRow' name='selectedRow' value=" + rst.getString(1) + ">");
			    	stringBuffer.append("</td>");
		    	}

		    	/*
		    	 * Code for printing the Column Values
		    	 */
		    	for (int i = 1; i <= count; i++) {
			    	stringBuffer.append("<td id='column" + z + i + "' style='" + this.defaultRowCssStyle() + "' class='" + this.rowStyle + "' ondblclick='toggleWrap(" + i + " , " + noOfRows + ")' nowrap='nowrap' >");
			    	if(rsmd.getColumnTypeName(i) == "DATE" || rsmd.getColumnTypeName(i) == "DATETIME")
			    		stringBuffer.append(rst.getInt(i));
			    	else{
			    		String data = rst.getString(i);
			    		if(data != null && data.startsWith("<")){
			    			stringBuffer.append(this.parseXMLString(data));
			    		}
			    		else{
			    			stringBuffer.append(data);
			    		}		    	
			    	}
		        	stringBuffer.append("</td>");
			   	}		
		    	stringBuffer.append("</tr>");
		    	z++;
		    }
		    
		    /*
		     * This will display a message when the table is without any rows.
		     */
		    if(!rst.first()) {
		    	stringBuffer.append("<tr>");
		    	stringBuffer.append("<td colspan=" + (count + 1) + " style='" + this.defaultRowCssStyle() + "' class='" + this.rowStyle + "'><span style='color:red;'>The table is empty.</span></td>");
		    	stringBuffer.append("</tr>");
		    }
		    
		    /*
		     * To get the number of rows count.
		     */
		    if(rowCountRequired) {
		    	stringBuffer.append("<tr>");
		    	stringBuffer.append("<td style='" + this.defaultFooterCssStyle() + "' class='" + this.footerStyle + "' colspan='" + (count + 1) + "'>");
		    	stringBuffer.append("Row Count: " + noOfRows);
		    	stringBuffer.append("</td>");
		    	stringBuffer.append("</tr>");
		    }
		} catch (SQLException ex) {
			throw new SQLException("SQLException occured while forming the paginated table: " + ex.getMessage());
		}

		return stringBuffer;
	}	
}
